package com.novellotus.iov.carvice.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/10/20.
 */

public class DiagnosisValueAdapter extends RecyclerView.Adapter {

    private List<RxDiagnosisAlarm.AlarmsBean.CausesBean> causesBeanList;
    private List<RxDiagnosisAlarm.AlarmsBean.SchemesBean> schemesBeanList;

    public DiagnosisValueAdapter(List<RxDiagnosisAlarm.AlarmsBean.CausesBean> causesBeanList, List<RxDiagnosisAlarm.AlarmsBean.SchemesBean> schemesBeanList) {
        this.causesBeanList = causesBeanList;
        this.schemesBeanList = schemesBeanList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_dialog_value, parent, false);
        return new ValueHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (causesBeanList != null) {
            initCauseHolder(position, (ValueHolder) holder);
        } else {
            initSchemesHolder(position, (ValueHolder) holder);
        }


    }


    private void initCauseHolder(final int position, final ValueHolder valueHolder) {
        valueHolder.tv_percent.setVisibility(View.VISIBLE);
        valueHolder.tv_value.setVisibility(View.VISIBLE);
        if (causesBeanList.get(position) != null) {
            valueHolder.tv_value.setText(causesBeanList.get(position).getCause());
            valueHolder.tv_percent.setText(causesBeanList.get(position).getUsageRate() + "%");
        }
        if(position==causesBeanList.size()-1){
            valueHolder.view_bottom.setVisibility(View.INVISIBLE);
        }
    }

    private void initSchemesHolder(final int position, final ValueHolder valueHolder) {
        if (schemesBeanList.get(position) != null) {
            valueHolder.tv_value.setText(schemesBeanList.get(position).getScheme());
            valueHolder.tv_percent.setText(schemesBeanList.get(position).getUsageRate() + "%");
        }
        if(position==schemesBeanList.size()-1){
            valueHolder.view_bottom.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        if (causesBeanList != null) {
            return causesBeanList.size();
        } else {
            return schemesBeanList.size();
        }

    }

    class ValueHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_value)
        TextView tv_value;
        @BindView(R.id.tv_percent)
        TextView tv_percent;
        @BindView(R.id.rlt_value)
        RelativeLayout rlt_value;
        @BindView(R.id.view_bottom)
        View view_bottom;

        ValueHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
